package com.class5.springboot.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * 对象和Map的转换对象工具类
 *
 * @author ccie
 *
 */
public class BeanMapConvert {

    public static <T> T mapToObject(Map<String, Object> fromMap, T target) throws Exception {
        return mapToObject(fromMap, target, null,false);
    }

    /**
     * 将map转换成对象,不设置静态和final的字段
     *
     * @param fromMap
     *            map来源
     * @param target
     *            要转化的对象
     * @param excludeProperties
     *            map中不设置到target的字段名称列表
     * @param nullCoverFlg
     *            map中空值 是否覆盖到对象中
     * @return
     * @throws Exception
     */
    public static <T> T mapToObject(Map<String, Object> fromMap, T target, String[] excludeProperties,boolean nullCoverFlg)
            throws Exception {
        if (fromMap == null) {
            return target;
        }
        Field[] fields = target.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            // 不设置静态和final的字段
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
            boolean update = true;
            if (excludeProperties != null) {
                for (String property : excludeProperties) {
                    if (property.equals(field.getName())) {
                        update = false;
                        break;
                    }
                }
            }
            if (update && fromMap.containsKey(field.getName())) {
                field.setAccessible(true);
                Object val = fromMap.get(field.getName());
                if(val == null){
                    if(nullCoverFlg){
                        field.set(target, val);
                    }else{
                        continue;
                    }
                }else{
                    Class typeClass = field.getType();
                    if(!typeClass.equals(String.class) && StringUtils.isTrimEmpty(val.toString())){
                        val = null;
                    }else{
                        if(typeClass.equals(Double.class)){
                            val = Double.valueOf(val.toString());
                        }else if(typeClass.equals(Integer.class)){
                            val = Integer.valueOf(val.toString());
                        }else if(typeClass.equals(Long.class)){
                            val = Long.valueOf(val.toString());
                        }else if(typeClass.equals(Float.class)){
                            val = Float.valueOf(val.toString());
                        }else if(typeClass.equals(java.sql.Date.class)){
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                            val = new java.sql.Date(format.parse(val.toString()).getTime());
                        }else if(typeClass.equals(java.sql.Timestamp.class)){
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            val = new java.sql.Timestamp(format.parse(val.toString()).getTime());
                        }
                    }
                    field.set(target, val);
                }
            }
        }
        return target;
    }

}